#include "function.h"
#include "message.h"
#include "node.h"
#include "vect.h"
#include "find.h"
#include "errors.h"
#include <fstream>

#define ELEMENTS_NO 10 
/* GLOBAL DECLARATIONS */

my_vect<node> *ob1 = new my_vect<node>(ELEMENTS_NO);

node test;
node *ptr;
char key;
size_t key1;

/* --------------------*/

void Start(){
	Message_Switch();
}

void Add(){	
	insert_ms();
    cin>>key;
	if(key=='1'){
		insert_ms_1();
        cin >> test;
		ob1->push(test);  
		}
	else if(key=='2'){
		size_t id;
		insert_ms_2(1);
		cin>>id;
		insert_ms_2(2);
		cin>>test;
		ob1->insert(test, --id);
	}
	else if(key=='3'){
		size_t id, no, i;
		insert_ms_3(1);
		cin >> id;
		insert_ms_3(2);
		cin >> no;
	node *temp = new node[no];
		for(i=0; i<no; i++) {
			insert_ms_3(3);
			cin>>temp[i];
		}
			ob1->insert(temp, --id, no);
		delete [] temp;
	}
}

void Del(){
	Disp();
	del_ms();
	cin>>key1;
	ob1->erase(key1);
}

void Dela(){
	dela_ms();
	cin>>key;
	if(key=='t' || key=='T')
		ob1->clear_all();
}

void Mod(){
	Disp();
	mod_ms(1);
	cin>>key1;
	mod_ms(2);
	cin>>test;
	ob1->edit(test, key1);	
}


void Fin(){
	ptr = ob1->get_begin();
	find_ms();
	cin>>test;	
	while(ptr){
		ptr = Finder::find(ptr, ob1->get_end(), test);
		if(ptr){
			std::cout<<*ptr++<<std::endl;
		}
	}
	system("pause");
}

void Save(){
	ofstream data_bin;
	data_bin.open("data.bin", ios::binary | ios::out);
	if(data_bin.good()){
		data_bin << *ob1;
		data_bin.close();
	}
	else
		error(ERR_OPEN_FILE);
}
void Read(){
	ifstream data_bin;
	data_bin.open("data.bin", ios::binary | ios::in);
	if(data_bin.good()){
		data_bin >> *ob1; 
		data_bin.close();
	}
	else
		error(ERR_OPEN_FILE);
}

void Disp(){
	disp_ms();
	ob1->disp();
	system("pause");
}

void Excel(){
	ob1->Prep_excel();
}

void Def(){
	def_ms();
}

size_t Exit(){
	delete [] ob1;
	return 0;
}
